package com.liuss.springchatai.service;

import com.liuss.springchatai.data.OrderRecord;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: lss
 * @Date: 2025-04-11-17:16
 * @Description:
 */
@Service
public class OrderRecordService {


    public OrderRecordService() {
        initExamRecords();
    }

    private static final String[] NAMES = {"张三", "李四", "王五", "赵六", "孙七", "孙悟空", "猪悟能", "唐三藏", "沙悟净", "白龙马"};
    private static final String[] DEPARTURE_STATIONS = {"北京西站", "上海南站", "广州东站", "深圳北站", "成都东站"};
    private static final String[] DESTINATION_STATIONS = {"重庆北站", "杭州东站", "武汉站", "南京南站", "西安北站"};
    private static final String[] STATUSES = {"待支付", "已支付", "退款中", "支付中", "退款中", "已退款", "已取消"};
    private static final Random random = new Random();
    private static final List<OrderRecord> orderRecords = new ArrayList<>();
    private static final String CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public void initExamRecords() {
        for (int i = 0; i < 10; i++) {
            OrderRecord record = new OrderRecord();
            record.setSort(i + 1);
            record.setOrderNumber(generateOrderNumber());
            record.setName(NAMES[random.nextInt(NAMES.length)]);
            record.setDepartureDate(new Date(System.currentTimeMillis() - random.nextInt(365 * 24 * 60 * 60 * 1000)));
            record.setDepartureStation(DEPARTURE_STATIONS[random.nextInt(DEPARTURE_STATIONS.length)]);
            record.setDestinationStation(DESTINATION_STATIONS[random.nextInt(DESTINATION_STATIONS.length)]);
            record.setOrderAmount(random.nextDouble() * 500 + 100);
            record.setStatus(STATUSES[random.nextInt(STATUSES.length)]);
            if ("已取消".equals(record.getStatus())) {
                record.setRefundAmount(record.getOrderAmount());
            } else {
                record.setRefundAmount(0);
            }
            orderRecords.add(record);
        }
    }

    private String generateOrderNumber() {
        StringBuilder sb = new StringBuilder("GG-");
        for (int i = 0; i < 13; i++) {
            int index = random.nextInt(CHAR_SET.length());
            sb.append(CHAR_SET.charAt(index));
        }
        return sb.toString();
    }

    public List<OrderRecord> list() {
        if (orderRecords.isEmpty()) {
            initExamRecords();
        }
        return orderRecords;
    }

    public boolean edit(OrderRecord orderRecord) {
        return false;
    }
}
